﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.EZBarTender;
using System.Configuration;
using Seagull.BarTender.Print;
using System.Windows.Forms;

namespace POSPlus.PrintManager
{
	public class RegularInfoWriter : EzbtWriter
	{
		private string _target;
		private List<PrintJob> _jobNames;
		private string _connectionString;
		private List<IRegular> _info;
		private string _labelLocation;

		public override string Target
		{
			get { return _target; }
			set { _target = value; }
		}
		public override List<PrintJob> JobName
		{
			get
			{ return _jobNames; }
			set
			{ _jobNames = value; }
		}
		public override string ConnectionString
		{
			get
			{ return _connectionString; }
			set
			{ _connectionString = value; }
		}
		public string LabelLocation
		{
			get { return _labelLocation; }
		}
		public List<IRegular> PrintInfo
		{
			get { return _info; }
		}

		public RegularInfoWriter(string connectionString, string labelLocation, List<PrintJob> jobName, string target)
		{
			ConnectionString = connectionString;
			JobName = jobName;
			Target = target;
			_labelLocation = labelLocation;
		}

		public override void Write()
		{
			BarTenderTreeBuilder builder = new BarTenderTreeBuilder(Target, "");
			builder.LabelLocation = _labelLocation;
			builder.StartTextData();
			String fields = "\"Description\",\"Vendor\",\"CircleFile\",\"Cert_Code\",\"AvgDailyMovement\",\"Pack\",\"Size\",\"Unit_NP\"," +
				"\"UOM\",\"RegularUpcCheckbit\",\"RegularUpcNoCheckbit\",\"RegularKehe\",\"Authorized\",\"RegularPriceLine1\",\"RegularPriceLine2\"," +
				"\"RegularPriceLine3\",\"UPC\",\"LocAisle\",\"LocBlock\",\"LocSeq\",\"PrintDate\"\n";
			builder.AppendTextData(fields);
			for (int i = 0; i < _info.Count; i++)
			{
				IRegular regularInfo = _info[i];
				string data = "";
				data += "\"" + regularInfo.Description + "\",";
				data += "\"" + regularInfo.Vendor.ToString() + "\",";
				data += "\"" + CircleFile(regularInfo.Vendor) + "\",";
				data += "\"" + regularInfo.CertCode.ToString() + "\",";
				data += "\"" + AvgDailyMovement(regularInfo.AvgDailyMovement) + "\",";
				data += "\"" + regularInfo.Pack.ToString() + "\",";
				data += "\"" + regularInfo.Size + "\",";
				data += "\"" + UnitNP(regularInfo.UnitNP) + "\",";
				data += "\"" + regularInfo.Uom + "\",";
				data += "\"" + RegularUpcCheckbit(regularInfo.Vendor, regularInfo.CertCode) + "\",";
				data += "\"" + RegularUpcNoCheckbit(regularInfo.Vendor, regularInfo.CertCode) + "\",";
				data += "\"" + RegularKehe(regularInfo.Vendor, regularInfo.Upc) + "\",";
				data += "\"" + Authorized(regularInfo.Hosted) + "\",";
				data += "\"" + PriceLine1(regularInfo.PriceMethod, regularInfo.NormalPrice, regularInfo.Quantity, regularInfo.GroupPrice) + "\",";
				data += "\"" + PriceLine2(regularInfo.PriceMethod, regularInfo.NormalPrice, regularInfo.Quantity, regularInfo.GroupPrice) + "\",";
				data += "\"" + PriceLine3(regularInfo.PriceMethod, regularInfo.NormalPrice, regularInfo.Quantity, regularInfo.GroupPrice) + "\",";
				data += "\"" + regularInfo.Upc + "\",";
				data += "\"" + regularInfo.LocAisle.ToString() + "\",";
				data += "\"" + regularInfo.LocBlock.ToString() + "\",";
				data += "\"" + regularInfo.LocSeq.ToString() + "\",";
				data += "\"" + DateTime.Now.ToString("MM/dd/yyyy") + "\",";
				data += "\"" + BatchNo(regularInfo.BatchNo) + "\"";
				if (i < _info.Count - 1)
					data += "\n";
				builder.AppendTextData(data);
			}

			builder.CloseTextData();

			using (Engine engine = new Engine(true))
			{
				Messages msgs = null;
				engine.XMLScript(Target, XMLSourceType.ScriptFile, out msgs);

				foreach (Seagull.BarTender.Print.Message msg in msgs)
					MessageBox.Show(msg.Text, msg.Source.ToString());

				engine.Stop();
			}
		}

		public override void GetInfo()
		{
			_info = new List<IRegular>();
			foreach (PrintJob job in _jobNames)
			{
				if (string.IsNullOrEmpty(job.BatchNo))
				{
					RegularPrintInfo reginfo = new RegularPrintInfo(ConnectionString);
					_info.AddRange(reginfo.Get(ConnectionString, job.JobName));
				}
				else
				{
					RegularBatchesPrintInfo reginfo = new RegularBatchesPrintInfo(ConnectionString);
					_info.AddRange(reginfo.Get(ConnectionString, job.JobName));
				}
			}
			_info.Sort();
		}

		private string BatchNo(string batchNo)
		{
			if(string.IsNullOrEmpty(batchNo))
				return "";

			return batchNo.Split('_')[0];
		}
	}
}